package com.wx.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wx.common.PageParam;
import com.wx.common.SecurityUser;
import com.wx.common.WrapMapper;
import com.wx.common.Wrapper;
import com.wx.domain.dto.ActionBindRoleDto;
import com.wx.domain.dto.CheckAuthorityCodeDto;
import com.wx.entity.SysAuthority;
import com.wx.holder.SecurityUserHolder;
import com.wx.service.SysAuthorityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>Title:      SysPermissionController. </p>
 * <p>Description 权限controller </p>
 * <p>Copyright: Copyright (c) 2016</p>
 * <p>Company:     陕西天璇信息科技有限公司</p>
 *
 * @author         <a href="xinglinglove1029@163.com"/>杨文生</a>
 * @since      2019/9/5 12:32
 */
@RestController
@RequestMapping(value = "/sysAuthority", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(tags = "权限管理", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class SysPermissionController{

    @Resource
    private SysAuthorityService authorityService;

    @Resource
    private SecurityUserHolder securityUserHolder;

    /**
     * <p>Title:	  listPage. </p>
     * <p>Description 分页查询权限列表</p>
     *
     * @param pageParam the PageParam<SysAuthority>
     * @return the wrapper
     * @Author <a href="xinglinglove1029@163.com"/>杨文生</a>
     * @CreateDate 2017 /8/17 19:04
     */
    @PostMapping(value = "/listPage")
    @ApiOperation(httpMethod = "POST", value = "分页查询权限列表")
    public Wrapper<PageInfo<SysAuthority>> listPage(@ApiParam(name = "authority", value = "权限信息") @RequestBody PageParam<SysAuthority> pageParam) {
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        List<SysAuthority> listPage = authorityService.queryListPage(pageParam.getParam());
        PageInfo<SysAuthority> pageInfo = new PageInfo<>(listPage);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, pageInfo);
    }


    /**
     * <p>Title:      delete. </p>
     * <p>Description 根据id删除权限</p>
     *
     * @param id the id
     * @return wrapper wrapper
     * @Author <a href="xinglinglove1029@163.com"/>杨文生</a>
     * @since 2018 /2/20 14:14
     */
    @PostMapping(value = "/delete/{id}")
    @ApiOperation(httpMethod = "POST", value = "根据id删除权限")
    public Wrapper<Integer> delete(@PathVariable String id) {
        SecurityUser user = securityUserHolder.getUser();
        int result = authorityService.deleteAuthorityById(id, user);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, result);
    }

    /**
     * <p>Title:      enable. </p>
     * <p>Description 启用权限</p>
     *
     * @param id the id
     * @return wrapper wrapper
     * @Author <a href="xinglinglove1029@163.com"/>杨文生</a>
     * @since 2018 /2/20 14:14
     */
    @PostMapping(value = "/enable/{id}")
    @ApiOperation(httpMethod = "POST", value = "启用权限")
    public Wrapper<Integer> enable(@PathVariable String id) {
        SecurityUser user = securityUserHolder.getUser();
        int result = authorityService.enableAuthorityById(id, user);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, result);
    }

    /**
     * <p>Title:      disable. </p>
     * <p>Description 禁用权限</p>
     *
     * @param id the id
     * @return wrapper wrapper
     * @Author <a href="xinglinglove1029@163.com"/>杨文生</a>
     * @since 2018 /2/20 14:14
     */
    @PostMapping(value = "/disable/{id}")
    @ApiOperation(httpMethod = "POST", value = "禁用权限")
    public Wrapper<Integer> disable(@PathVariable String id) {
        SecurityUser user = securityUserHolder.getUser();
        int result = authorityService.disableAuthorityById(id, user);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, result);
    }

    /**
     * <p>Title:      modifyAuthority. </p>
     * <p>Description 修改权限信息</p>
     *
     * @param authority the authority
     * @return wrapper wrapper
     * @Author <a href="xinglinglove1029@163.com"/>杨文生</a>
     * @since 2018 /2/20 15:38
     */
    @PostMapping(value = "/modifyAuthority")
    @ApiOperation(httpMethod = "POST", value = "修改权限信息")
    public Wrapper<Integer> modifyAuthority(@ApiParam(name = "authority", value = "用户信息") @RequestBody SysAuthority authority) {
        SecurityUser user = securityUserHolder.getUser();
        int result = authorityService.modifyAuthority(authority, user);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, result);
    }

    /**
     * <p>Title:      saveAuthorityInfo. </p>
     * <p>Description 保存权限信息</p>
     *
     * @param authority the authority
     * @return wrapper wrapper
     * @Author <a href="xinglinglove1029@163.com"/>杨文生</a>
     * @since 2018 /2/22 17:24
     */
    @PostMapping(value = "/saveAuthorityInfo")
    @ApiOperation(httpMethod = "POST", value = "保存权限信息")
    public Wrapper<Integer> saveAuthorityInfo(@ApiParam(name = "authority", value = "权限信息") @RequestBody SysAuthority authority) {
        SecurityUser user = securityUserHolder.getUser();
        int result = authorityService.saveAuthorityInfo(authority, user);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, result);
    }

    /**
     * <p>Title:      checkAuthorityCode. </p>
     * <p>Description 校验权限编码唯一性</p>
     *
     * @param checkAuthorityCodeDto the check authority code dto
     * @return wrapper wrapper
     * @Author <a href="xinglinglove1029@163.com"/>杨文生</a>
     * @since 2018 /2/24 17:08
     */
    @PostMapping(value = "/checkAuthorityCode")
    @ApiOperation(httpMethod = "POST", value = "校验权限编码唯一性")
    public Wrapper<Boolean> checkAuthorityCode(@ApiParam(name = "checkAuthorityCodeDto", value = "权限编码dto") @RequestBody CheckAuthorityCodeDto checkAuthorityCodeDto) {
        boolean flag = false;
        String authorityId = checkAuthorityCodeDto.getAuthorityId();
        String authorityCode = checkAuthorityCodeDto.getAuthorityCode();
        Example example = new Example(SysAuthority.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("authorityCode", authorityCode);
        if (StringUtils.isNotEmpty(authorityId)) {
            criteria.andNotEqualTo("id", authorityId);
        }
        int result = authorityService.selectCountByExample(example);
        if (result > 0) {
            flag = true;
        }
        return WrapMapper.ok(flag);
    }


    /**
     * <p>Title:      getBindRoleListByActionId. </p>
     * <p>Description 查詢当前权限字符串所绑定的角色信息</p>
     *
     * @param actionId String
     * @return Wrapper
     * @Author        <a href="xinglinglove1029@163.com"/>杨文生</a>
     * @since     2019/4/11 14:35
     */
    @PostMapping(value = "/getBindRoleListByActionId/{actionId}")
    @ApiOperation(httpMethod = "POST", value = "查詢当前权限字符串所绑定的角色信息")
    public Wrapper<List<String>> getBindRoleListByActionId(@PathVariable String actionId) {
        List<String> bindRoleList = authorityService.getBindRoleListByActionId(actionId);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, bindRoleList);
    }

    /**
     * <p>Title:      bindRole. </p>
     * <p>Description 权限绑定角色</p>
     *
     * @param         actionBindRoleDto ActionBindRoleDto
     * @return        Wrapper
     * @Author        <a href="xinglinglove1029@163.com"/>杨文生</a>
     * @since     2019/4/11 17:23
     */
    @PostMapping(value = "/bindRole")
    @ApiOperation(httpMethod = "POST", value = "权限绑定角色")
    public Wrapper<?> bindRole(@RequestBody  @Validated() ActionBindRoleDto actionBindRoleDto) {
        authorityService.bindRole(actionBindRoleDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE);
    }
}
